<template>
  <div class="app-container calendar-list-container">
    <div class="filter-container">
      <el-input v-model="listQuery.name" style="width: 200px;" class="filter-item" placeholder="请输入资产名称" @keyup.enter.native="handleFilter"/>
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">{{ $t('table.search') }}</el-button>
    </div>

    <el-table
      v-loading="listLoading"
      :key="tableKey"
      :data="list"
      element-loading-text="给我一点时间"
      border
      fit
      highlight-current-row
      style="width: 100%">
      <el-table-column width="200" align="center" label="测量ID">
        <template slot-scope="scope">
          <span>{{ scope.row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column width="110" align="center" label="测量摘要">
        <template slot-scope="scope">
          <span>{{ scope.row.measurementsSummary }}</span>
        </template>
      </el-table-column>
      <el-table-column width="110" align="center" label="站点ID">
        <template slot-scope="scope">
          <span>{{ scope.row.siteToken }}</span>
        </template>
      </el-table-column>
      <el-table-column width="110" align="center" label="接收时间">
        <template slot-scope="scope">
          <span>{{ scope.row.receivedDate }}</span>
        </template>
      </el-table-column>
      <el-table-column width="110" align="center" label="测量时间">
        <template slot-scope="scope">
          <span>{{ scope.row.eventDate }}</span>
        </template>
      </el-table-column>

      <el-table-column :label="$t('table.actions')" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="handleDetails(scope.row)">{{ $t('table.details') }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <div class="pagination-container">
      <el-pagination
        :current-page.sync="listQuery.page"
        :page-sizes="[10,20,30, 50]"
        :page-size="listQuery.limit"
        :total="total"
        background
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"/>
    </div>

    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form ref="dataForm" :rules="rules" :model="temp" label-position="left" label-width="100px" style="width: 400px; margin-left:50px;">
        <el-form-item label="资产名称" prop="alternateId">
          <el-input v-model="temp.alternateId"/>
        </el-form-item>
        <el-form-item label="资产ID" prop="assetId">
          <el-input v-model="temp.assetId"/>
        </el-form-item>
        <el-form-item label="资产分类ID" prop="assetModuleId">
          <el-input v-model="temp.assetModuleId"/>
        </el-form-item>
        <el-form-item label="是否绑定" prop="assignmentType">
          <el-input v-model="temp.assignmentType"/>
        </el-form-item>
        <el-form-item label="设备指派token" prop="deviceAssignmentToken">
          <el-input v-model="temp.deviceAssignmentToken"/>
        </el-form-item>
        <el-form-item label="事件发生时间" prop="eventDate">
          <el-input v-model="temp.eventDate"/>
        </el-form-item>
        <el-form-item label="事件类型" prop="eventType">
          <el-input v-model="temp.eventType"/>
        </el-form-item>
        <el-form-item label="测量ID" prop="id">
          <el-input v-model="temp.id"/>
        </el-form-item>
        <el-form-item label="测量数据" prop="measurements">
          <el-input v-model="temp.measurements"/>
        </el-form-item>
        <el-form-item label="测量摘要" prop="measurementsSummary">
          <el-input v-model="temp.measurementsSummary"/>
        </el-form-item>
        <el-form-item label="元数据" prop="metadata">
          <el-input v-model="temp.metadata"/>
        </el-form-item>
        <el-form-item label="事件接收时间" prop="receivedDate">
          <el-input v-model="temp.receivedDate"/>
        </el-form-item>
        <el-form-item label="站点token" prop="siteToken">
          <el-input v-model="temp.siteToken"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">{{ $t('table.cancel') }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

import { assetEvent } from '@/api/fire/asset/asset'
import waves from '@/directive/waves' // 水波纹指令

export default {
  name: 'Measure',
  directives: {
    waves
  },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'info',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: null,
      listLoading: true,
      listQuery: {
        name: '',
        siteid: '',
        assetid: this.$route.params.assetid,
        page: 1,
        pagesize: 10,
        startdate: '2019-02-19',
        enddate: '2019-03-19',
        ismeasurement: 'YES',
        islocation: 'NO',
        isalert: 'NO'
      },
      showReviewer: false,
      temp: {
        alternateId: '',
        assetId: '',
        assetModuleId: '',
        assignmentType: '',
        deviceAssignmentToken: '',
        eventDate: '',
        eventType: '',
        id: '',
        measurements: '',
        measurementsSummary: '',
        metadata: '',
        receivedDate: '',
        siteToken: ''
      },
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        details: '详情'
      },
      dialogPvVisible: false,
      pvData: [],
      dialogFormRolesVisible: false,
      btn_query: false,
      btn_add: false,
      btn_edit: false,
      btn_delete: false
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      assetEvent(this.listQuery).then(response => {
        console.log(this.listQuery)
        this.list = response.data.context.results
        this.total = response.data.context.numResults
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleSizeChange(val) {
      this.listQuery.pagesize = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.listQuery.page = val
      this.getList()
    },
    handleModifyStatus(row, status) {
      this.$message({
        message: '操作成功',
        type: 'success'
      })
      row.status = status
    },
    handleDetails(row) {
      this.temp = Object.assign({}, row) // copy obj
      this.dialogStatus = 'details'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    }
  }
}
</script>
